﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace FolderAlias
{
    public partial class Form1 : Form
    {
        private string path = string.Empty;
        private string DesktopFilePath;
        private const string DesktopFileName = "desktop.ini";
        private const string ProfileSection = ".ShellClassInfo";
        private const string ProfileKeyName = "LocalizedResourceName";
        private const string ProfileKeyIcon = "IconResource";

        #region API函数声明
        [DllImport("kernel32")]//返回0表示失败，非0为成功
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]//返回取得字符串缓冲区的长度
        private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        #endregion

        public Form1()
        {
            InitializeComponent();
        }
        
        #region 选择文件夹/拉入文件夹后需要更新的数据
        private void updateStatus() {
            path = folderBrowserDialog.SelectedPath;
            toolStripStatusLabel2.Text = path;
            DesktopFilePath = Path.Combine(this.path, DesktopFileName);
            if (File.Exists(DesktopFilePath))
            {
                this.textBox1.Text = getProfileString(ProfileKeyName);
            }
        }
        #endregion

        #region 按钮点击事件-选择文件夹
        private void btn_selectFolder_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = folderBrowserDialog.ShowDialog();
            if (dialogResult == DialogResult.OK) {
                updateStatus();
            }
        }
        #endregion

        #region 按钮点击事件-修改文件夹别名
        private void btn_change_Click(object sender, EventArgs e)
        {
            if (this.path == string.Empty) {
                MessageBox.Show("你还没有选择文件夹!", "提示");
                return;
            }
            bool ret = setProfileString(ProfileKeyName, this.textBox1.Text);
            if (ret) {
                FileInfo fileInfo = new FileInfo(DesktopFilePath);
                fileInfo.Attributes = FileAttributes.Hidden;
                MessageBox.Show("修改成功,若未变化请等待一段时间或者重启电脑!", "提示");
            }
            
        }
        #endregion

        #region 获取配置文件值
        private string getProfileString(string key)
        {
            StringBuilder temp = new StringBuilder(1024);
            GetPrivateProfileString(ProfileSection, key, null, temp, 1024, DesktopFilePath);
            return temp.ToString();
        }
        #endregion

        #region 设置配置文件值
        private bool setProfileString(string key, string val)
        {
            long ret = WritePrivateProfileString(ProfileSection, key, val, DesktopFilePath);
            if (ret == 0)
            {
                return false;
            }
            else {
                return true;
            }
        }
        #endregion

        #region 设置允许拉入的类型
        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }
        #endregion

        #region 拉入文件夹的响应事件
        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            Array array = (Array)e.Data.GetData(DataFormats.FileDrop);
            if (array.Length != 0) {
                if (Directory.Exists(array.GetValue(0).ToString())) {
                    folderBrowserDialog.SelectedPath = array.GetValue(0).ToString();
                    updateStatus();
                }
                
            }
            
        }
        #endregion
    }
}
